package com.vastcom.cms.action;

import com.vastcom.cms.model.Carousel;
import com.vastcom.cms.model.News;
import com.vastcom.cms.model.Singlepage;
import com.vastcom.cms.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;

@CrossOrigin
@RestController
@PreAuthorize("hasAuthority('admin')")
@RequestMapping("/manage")
public class AdminAction {
    @Autowired
    private ManageService manageService;
    @Value("${uploadlocation}")
    private String uploadfilelocation;


    @PostMapping("saveSinglePage")
    @ResponseBody
    public HashMap saveSinglePage(@RequestBody Singlepage singlepage)throws Exception{
        HashMap m=new HashMap();
        try {
            manageService.saveSinglepage(singlepage);

            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }

        return m;
    }

    @PostMapping("deleteSinglePage")
    @ResponseBody
    public HashMap deleteSinglePage(@RequestBody Singlepage singlepage)throws Exception {
        HashMap m = new HashMap();

        try {
            manageService.deleteSinglepage(singlepage.getSinglepageid());
            m.put("msg", "ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }
        return m;
    }

    @PostMapping("getSinglepage")
    @ResponseBody
    public HashMap getSinglepage(@RequestBody Singlepage singlepage)throws Exception{
        HashMap m=new HashMap();
        Singlepage temp=manageService.getSinglepage(singlepage.getSinglepageid());
        if(temp!=null){
            m.put("msg","ok");
            m.put("obj",temp);
        }
        else {
            m.put("msg","empty");
        }
        return m;
    }





    @PostMapping("saveNews")
    @ResponseBody
    public HashMap saveNews(@RequestBody News news)throws Exception{
        HashMap m=new HashMap();
        try {
            news.setPbdate(new Timestamp(System.currentTimeMillis()).getTime());
            manageService.saveNews(news);

            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }

        return m;
    }


    @PostMapping("deleteNews")
    @ResponseBody
    public HashMap deleteNews(@RequestBody News news)throws Exception{
        HashMap m=new HashMap();

        try {
            manageService.deleteNews(news.getNewsid());
            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }


        return m;
    }

    //图片上传
    @PostMapping("uploadPic")
    @ResponseBody
    public HashMap uploadPic(@RequestBody MultipartFile uploadfile) throws Exception{
        HashMap m=new HashMap();

        String finename=uploadfile.getOriginalFilename();
        String suffixname=uploadfile.getOriginalFilename().substring(finename.lastIndexOf("."));

        finename=String.valueOf(System.currentTimeMillis())+suffixname;


        //String filepath=uploadfilelocation;
        String filepath="/usr/server/cms/upload/";

        File tf=new File(filepath);
        if(!tf.exists()){
            tf.mkdir();
        }




        try {
            uploadfile.transferTo(new File(filepath+finename));
            m.put("msg","ok");
            m.put("filename",finename);
            return m;
        } catch (IOException e) {
            e.printStackTrace();
            m.put("msg","ioerror");
        } catch (IllegalStateException e) {
            e.printStackTrace();
            m.put("msg","illeageerror");
        }

        return m;
    }


    @PostMapping("saveCarousel")
    @ResponseBody
    public HashMap saveCarousel(@RequestBody Carousel carousel)throws Exception{
        HashMap m=new HashMap();
        try {
            manageService.saveCarousel(carousel);
            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }
        return m;
    }


    @PostMapping("deleteCarousel")
    @ResponseBody
    public HashMap deleteCarousel(@RequestBody Carousel carousel)throws Exception{
        HashMap m=new HashMap();
        try {
            manageService.deleteCarousel(carousel.getCarouselid());
            m.put("msg","ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }
        return m;
    }




    @PostMapping("getCarousel")
    @ResponseBody
    public HashMap getCarousel(@RequestBody Carousel carousel)throws Exception{
        HashMap m=new HashMap();
        Carousel temp=manageService.getCarousel(carousel.getCarouselid());
        if(temp!=null){
            m.put("msg","ok");
            m.put("obj",temp);
        }
        else {
            m.put("msg","empty");
        }
        return m;
    }


}
